package com.dj.uphome.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.dj.common.web.controller.CoreCrudRestController;
import com.dj.common.web.rest.ResponsePack;
import com.dj.common.web.vo.EasyuiPage;
import com.dj.uphome.web.service.system.SysMenuService;
import com.dj.uphome.web.vo.system.SysMenuVo;
import com.dj.uphome.web.vo.system.SysRoleVo;

/**
 * 类名称：系统菜单
 */
@RestController
@RequestMapping(value = "/menu")
public class MenuController extends CoreCrudRestController{
	@Autowired
	private SysMenuService sysMenuService;
	
	/**
	 * 访问系统首页
	 */
	@PostMapping(value = "/currentMenu")
	public ResponseEntity<ResponsePack<List<SysMenuVo>>> currentMenu(@RequestParam int userId) throws Exception {	
		List<SysMenuVo> userMenus = sysMenuService.listAllByUserId(userId);
		List<SysMenuVo> allmenuList = new ArrayList<SysMenuVo>();
		if (null != userMenus) {
			for (SysMenuVo menu : userMenus) {
				if (!allmenuList.contains(menu)) {
					if (menu.getParentId() == 0) {
						List<SysMenuVo> submenuList = new ArrayList<SysMenuVo>();
						for (SysMenuVo sub : userMenus) {
							if (menu.getMenuId() == sub.getParentId()) {
								submenuList.add(sub);
							}
						}
						menu.setSubMenu(submenuList);
						allmenuList.add(menu);
					}
				}
			}
		}
		return new ResponseEntity<>(new ResponsePack<>(allmenuList), HttpStatus.OK);
	}
	/**
	 * 显示菜单列表
	 * 
	 * @param model
	 * @return
	 */
	@PostMapping(value = "/list")
	public ResponseEntity<ResponsePack<List<SysMenuVo>>> listParent(@RequestParam(defaultValue="0") int menuId) throws Exception {
		SysMenuVo vo = new SysMenuVo();
		vo.setMenuId(menuId);
		return new ResponseEntity<>(new ResponsePack<>(sysMenuService.findAll(vo)), HttpStatus.OK);
	}
	/*
	*//**
	 * 显示用户列表(用户组)EasyuiPage
	 *//*
	@RequestMapping(value = "/listTree", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ResponseEntity<List<SysMenuVo>> listTree(EasyuiPage<SysRoleVo> page, SysMenuVo vo) throws Exception {
		List<SysMenuVo> menus = sysMenuService.listTree();
		return new ResponseEntity<>(menus, HttpStatus.OK);
	}*/

	/**
	 * 显示树形
	 */
	@RequestMapping(value = "/tree")
	public ModelAndView tree() throws Exception {
		ModelAndView mv = this.getModelAndView();
		try {
			String json = JSONArray.toJSONString(sysMenuService.listTree());
			json = json.replaceAll("scienceId", "id").replaceAll("buttons", "children").replaceAll("menus", "children")
					.replaceAll("menuName", "name").replaceAll("buttonName", "name").replaceAll("subMenu", "children")
					.replaceAll("hasMenu", "checked");
			mv.addObject("tree", json);
			mv.setViewName("system/menu/menu_list");
		} catch (Exception e) {
			mv.addObject("exception", e);
			mv.setViewName(defultError);
		}
		return mv;
	}
}
